package com.aj.service.impl;

import com.aj.entity.TAddRequest;
import com.aj.entity.TDeleteRequest;
import com.aj.entity.Tag;
import com.aj.mapper.TagMapper;
import com.aj.service.TagService;
import com.aj.util.Result;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author aj
 * @since 2020-08-03
 */
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {

	@Autowired
	TagMapper tm;
	@Override
	public Result getTagList() {
		// TODO Auto-generated method stub
		try {
			return new Result(true,"成功辽",200,tm.getList());
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return new Result(false,"失败辽",500,null);
		}
	}
	@Override
	@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
	public Result addTag(TAddRequest t) {
		// TODO Auto-generated method stub
		try {
//			System.out.println(t.toString());
			Long tid=tm.getIdByValue(t.getValue());
			System.out.println("tid:"+tid);
			
			if(tid!=null) {//tid已存在，直接添加到联系表
//				System.out.println("6666666666");
				tm.addATag(new TAddRequest(t.getAid(),tid,null));
				return new Result(true,"成功辽",200,new Tag(tid,t.getValue()));
			}
			else {//tid不存在，需要先插入实体表，再插入联系表
				Tag tag=new Tag(0,t.getValue());
				tm.addTag(tag);
//				int x=0/0;
				tm.addATag(new TAddRequest(t.getAid(),tag.getTid(),null));
				return new Result(true,"成功辽",200,new Tag(tag.getTid(),t.getValue()));
			}
			
		}
		catch(Exception e)
		{
			System.out.println("出错了md");
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			//mybatis回滚必须要出现未被捕捉的运行时错误才会生效，因此要手动回滚
			e.printStackTrace();
			return new Result(false,"失败辽",500,null);
		}
	}
	@Transactional
	@Override
	public Result deleteTag(TDeleteRequest t) {
		// TODO Auto-generated method stub
		try {
			tm.deleteById(t.getTid());//删除tag表中的记录
//			int x=1/0;
			tm.deleteRelation(t);//删除a_tag表中的记录 aid-tid键值对
			return new Result(true,"成功辽",200,null);
		}
		catch(Exception e)
		{
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			e.printStackTrace();
			return new Result(false,"失败辽",500,null);
		}
	}
	@Override
	public Result updateTag(Tag t) {
		// TODO Auto-generated method stub
		try {
			return new Result(true,"成功辽",200,tm.updateById(t));
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return new Result(false,"失败辽",500,null);
		}
	}
	@Override
	public Result getTagByAid(long aid) {
		// TODO Auto-generated method stub
		try {
			return new Result(true,"成功辽",200,tm.getTagByAid(aid));
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return new Result(false,"失败辽",500,null);
		}
	}

}
